package com.xliang.db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class DBUtil
{
    private static final String driverName = "com.mysql.jdbc.Driver";
    
    private static final String username = "root";
    
    private static final String password = "123456";
    
    private static final String url =
        "jdbc:mysql://127.0.0.1:3306/xliang?useUnicode=true&characterEncoding=UTF8&zeroDateTimeBehavior=convertToNull&autoReconnect=true&allowMultiQueries=true";
    
    public static Connection open()
    {
        try
        {
            Connection connection = null;
            if (connection == null)
            {
                Class.forName(driverName);
                connection = DriverManager.getConnection(url, username, password);
            }
            return connection;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return null;
        }
    }
    
    public static void close(Connection conn)
    {
        try
        {
            if (conn != null)
            {
                conn.close();
            }
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }
    }
    
    public static void close(PreparedStatement pst)
    {
        try
        {
            if (pst != null)
            {
                pst.close();
                
            }
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }
    }
    
    public static void close(ResultSet rs)
    {
        try
        {
            if (rs != null)
            {
                rs.close();
                
            }
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }
    }
}
